//
//  SinaBaseVistorView.swift
//  SinaWeiBo
//
//  Created by 刘世瑛 on 16/1/21.
//  Copyright © 2016年 刘世瑛. All rights reserved.
//

import UIKit
protocol SinaBaseVistorViewDelegate: NSObjectProtocol{
    
    func didLogInMethod()
    func didRegisterMethod()
}
class SinaBaseVistorView: UIView {

   weak var delegate:SinaBaseVistorViewDelegate?
    
   
    func setUpInfor(info:String,image:String?){
        if let img = image {
            animationImageView.hidden = true
            coverImage.hidden = true
            houseImageView.image = UIImage(named: img)
        }else{
       
        message.text = info
        startAnimat()
        }
    }
    
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    
    private func startAnimat(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.removedOnCompletion = false

        animationImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    private lazy var animationImageView:UIImageView = {
       let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
    
        
       img.sizeToFit()
        return img
        
        
    }()
    
   private lazy var houseImageView : UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        img.sizeToFit()
        return img
    }()
    
    
    private lazy var message : UILabel  = {
       let label = UILabel()
        label.text = "登录微博显示微博数据，如果没有帐号请先注册"
        label.textColor = UIColor.grayColor()
        label.numberOfLines = 0
       
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(14)
        
        return label
        
    }()
    
    private lazy var registerButton : UIButton =  {
        let bt = UIButton()
        bt.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        bt.setTitle("注册", forState: .Normal)
        
        bt.setTitleColor(UIColor.blackColor(), forState: .Normal)
        
        bt.addTarget(self, action: "didClickRegister", forControlEvents: .TouchUpInside)
        
        return bt
    }()
    
   private lazy var logInButton : UIButton =  {
        let bt = UIButton()
        bt.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        bt.setTitle("登录", forState: .Normal)
        bt.setTitleColor(UIColor.blackColor(), forState: .Normal)
        
        bt.addTarget(self, action: "didClickLogIn", forControlEvents: .TouchUpInside)

        
        return bt
    }()
  
    private lazy var coverImage:UIImageView = {
       
        let img  = UIImageView()
        img.image = UIImage(named:"visitordiscover_feed_mask_smallicon")
        img.sizeToFit()
        print(img)
        return img
        
    }()
    
    
    @objc private func didClickRegister()
    {
        delegate?.didRegisterMethod()
        
    }
    
    @objc private func didClickLogIn()
    {
        delegate?.didLogInMethod()
        
    }
}
extension SinaBaseVistorView
{
    private func setUpUI()
    {
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        addSubview(animationImageView)
        animationImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(coverImage)
        coverImage.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(houseImageView)
        houseImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(message)
        message.translatesAutoresizingMaskIntoConstraints = false
        
     
        
        addSubview(registerButton)
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(logInButton)
        logInButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverImage, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImage, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImage, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImage, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Height, multiplier: 1, constant: -500))

        
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .CenterX, relatedBy: .Equal, toItem: animationImageView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .CenterY, relatedBy: .Equal, toItem: animationImageView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: message, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        addConstraint(NSLayoutConstraint(item: message, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: message, attribute: .TopMargin, relatedBy: .Equal, toItem: animationImageView, attribute: .Bottom, multiplier: 1, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 30))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: message , attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .TopMargin, relatedBy: .Equal, toItem: message, attribute: .Bottom, multiplier: 1, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: logInButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: logInButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 30))
        addConstraint(NSLayoutConstraint(item: logInButton, attribute: .Right, relatedBy: .Equal, toItem: message , attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: logInButton, attribute: .TopMargin, relatedBy: .Equal, toItem: message, attribute: .Bottom, multiplier: 1, constant: 10))
        
       

        
    }
    

    
}
